//package cn.itsource.zuul1020.config;
//
//import cn.itsource.domain.login.LoginInfo;
//import cn.itsource.domain.login.Permission;
//import cn.itsource.zuul1020.mapper.LoginInfoMapper;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.authority.SimpleGrantedAuthority;
//import org.springframework.security.core.userdetails.User;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
//import org.springframework.stereotype.Service;
//import org.springframework.util.StringUtils;
//
//import javax.annotation.Resource;
//import java.util.ArrayList;
//import java.util.List;
//
//@Service
//public class JdbcUserDetailsService implements UserDetailsService {
//
//    @Resource
//    private LoginInfoMapper loginMapper;
//
//    @Override
//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        //1.username基本判断
//        if(!StringUtils.hasLength(username)){
//            throw new UsernameNotFoundException("用户名不可为空");
//        }
//        //2.根据username查询mysql中的认证信息
//        LoginInfo dbUser = loginMapper.findByUsername(username);
//        if(dbUser == null){
//            throw new UsernameNotFoundException("用户名不存在");
//        }
//
//        //3.加载用户的权限列表
//        List<GrantedAuthority> authorities = new ArrayList<>();
//        Integer byRoleId = loginMapper.findByRoleId(dbUser.getId());
//        List<String> byPermissions = loginMapper.findByPermissions(byRoleId);
//        for (String permission : byPermissions){
//            //GrantedAuthority = empl:list
//            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(permission);
//            System.out.println("加载用户【" + username + "】的权限：" + permission);
//            authorities.add(simpleGrantedAuthority);
//        }
//
//        //4.把认证信息和权限信息封装成UserDetails返回
//        User userDetails = new User(username, dbUser.getPassword(), true,
//                true, true, true, authorities);
//        return userDetails;
//    }
//
//}